﻿#ifndef __IBT_MSGMGR_H__
#define __IBT_MSGMGR_H__

#include <semaphore.h>

#include "IBT_Common.h"
#include "IBT_List.h"

#define MAX_RECVD_MSG_CNT			32
#define MAX_RECVD_MSG_BUF_SIZE		128
#define MAX_MSG_NAME_SIZE			32
#define IBT_SENDMSG_TIMEOUT_MS		2000

typedef enum {
	IBT_INV_Msg_Type						= 0,
	IBT_First_Msg_Type						= 1,
	IBT_Add_IPC								= IBT_First_Msg_Type,
	IBT_Add_IPC_Manual						= IBT_Add_IPC,	/* default */
	IBT_Add_IPC_Sound_Wave_Pair_Code,
	IBT_Add_IPC_Scan_QR_Code,
	IBT_Add_IPC_Host_AP,
	IBT_Add_IPC_LAN_Scan,
	IBT_Add_IPC_History_Scan,

	IBT_UART,
	IBT_UART_WKUP							= IBT_UART,		/*default*/
	IBT_UART_BATR,
	IBT_UART_STDN,
	IBT_UART_STAT,
	IBT_UART_BATQ,
	IBT_UART_REST,
	IBT_UART_FLSH,

	IBT_SYSTEM,
	IBT_SYSTEM_EVENT_POWER_OFF				= IBT_SYSTEM,

	IBT_Video,
	IBT_Video_Play							= IBT_Video,	/* default*/
	IBT_Video_Pause,
	IBT_Video_Stop,
	IBT_VIDEO_SET_RESOLUTION,
	IBT_VIDEO_GET_RESOLUTION,
	IBT_VIDEO_SET_FILP,
	IBT_VIDEO_GET_FILP,
	IBT_VIDEO_SET_IRCUT,
	IBT_VIDEO_GET_IRCUT,
	IBT_VIDEO_REQUEST_IDR,
	IBT_VIDEO_SET_BITRATE,
	IBT_VIDEO_GET_BITRATE,
	IBT_VIDEO_CHECK_VI_STATUS,
	IBT_VIDEO_SET_ENCODER_TYPE,
	IBT_VIDEO_GET_ENCODER_TYPE,
	IBT_VIDEO_GET_ENCODER_JPEG,

	IBT_Audio,
	IBT_Audio_Play							= IBT_Audio,	/* default */
	IBT_Audio_Pause,
	IBT_Audio_Stop,
	IBT_AUDIO_SET_PLAY_VOL,
	IBT_AUDIO_GET_PLAY_VOL,

	IBT_Speaker,
	IBT_Speaker_Play						= IBT_Speaker,	/* default */
	IBT_Speaker_Pause,
	IBT_Speaker_Stop,

	IBT_Record,
	IBT_Record_Play							= IBT_Record,	/* default */
	IBT_Record_Pause,
	IBT_Record_Stop,

	IBT_P2P,
	IBT_P2P_AUDIO_SEND						= IBT_P2P, /* default */
	IBT_P2P_VIDEO_SEND,
	IBT_P2P_AUDIO_RECORD_RELAY,
	IBT_P2P_VIDEO_RECORD_RELAY,
	IBT_P2P_RECORD_RELAY_START,
	IBT_P2P_RECORD_RELAY_STOP,
	IBT_P2P_GET_HEARTBEAT,
	IBT_P2P_GET_WAKEUP_PATTERN,

	IBT_PUSH,
	IBT_PUSH_MSG							= IBT_PUSH, /* default */

	IBT_PROFILES,
	IBT_PROFILES_READ						= IBT_PROFILES, /* default */
	IBT_PROFILES_WRITE,
	IBT_PROFILES_GET_STATUS,

	IBT_ALARM,
	IBT_ALARM_PIR							= IBT_ALARM,
	IBT_ALARM_KEY,
	IBT_ALARM_NEW,
	IBT_ALARM_DEL,
	IBT_ALARM_ENABLE,
	IBT_ALARM_DISABLE,
	IBT_ALARM_GET_INFO,
	IBT_ALARM_HANDLER,
	IBT_ALARM_UPDATE,

	IBT_POWER,
	IBT_POWER_ON							= IBT_POWER,
	IBT_POWER_OFF,
	IBT_POWER_WAKELOCK,
	IBT_POWER_WAKEUNLOCK,
	IBT_POWER_SET_PIR,
	IBT_POWER_GET_PIR,
	IBT_POWER_GET_WIFI_STATUS,
	IBT_POWER_GET_MCU_VERSION,
	IBT_POWER_GET_CAMERA_VERSION,
	IBT_POWER_GET_BATTERY,
	IBT_POWER_SET_POWEROFF_LVL,

	IBT_MP4_RECORD,
	IBT_MP4_RECORD_CREATE					= IBT_MP4_RECORD,
	IBT_MP4_RECORD_SAVE_AUDIO,
	IBT_MP4_RECORD_SAVE_VIDEO,
	IBT_MP4_RECORD_GET_STATUS,
	IBT_MP4_RECORD_STOP,
	IBT_MP4_RECORD_CHANGENAME,
	IBT_MP4_RECORD_SETRECORDTIME,

	IBT_FILTER,
	IBT_FILTER_MOVE							= IBT_FILTER,
	IBT_FILTER_PIR_FAST_STOP,

	IBT_FB,
	IBT_FB_DISPLAY                          = IBT_FB,

	IBT_Max_Msg_Type,
} IBTMsgType;

typedef enum {
	IBT_MSG_STATUS_UNREGISTER,
	IBT_MSG_STATUS_REGISTER,
} IBTMsgRegStatus;

typedef enum {
	IBT_MSG_STATUS_EMPTY,
	IBT_MSG_STATUS_USAGE,
} IBTMsgProcStatus;

typedef struct {
	IBTMsgType			msgType;
	IBT_CHAR			*msgRecvBuf;
	IBT_UI				msgRecvSize;
} IBTMsgRecvInfoCtx;

typedef struct {
	IBTList				msgRecvList;
	IBTMsgRecvInfoCtx	msgRecvInfoCtx;
} IBTMsgRecvInfo;

typedef struct {
	IBTMsgRecvInfo		*msgRecvInfo;
	IBT_UI				msgRecvInfoCnt;
	IBT_UI				msgRecvBufSize;

	IBTListQueue		msgRecvInitQueue;
	sem_t				msgRecvInitSem;
	IBTListQueue		msgRecvDoneQueue;
	sem_t				msgRecvDoneSem;
} IBTMsgRecv;

typedef struct IBTMsgInfo IBTMsgInfo;
typedef IBT_INT (*IBTMsgProcessFunc)(IBTHandler *ibtHandler, IBT_PTR param, IBT_INT paramSize, IBT_PTR priv);
typedef IBT_INT (*IBTBufToMsgParamFunc)(IBTMsgInfo *msgInfo, IBT_CHAR *msgRecvBuf, IBT_UI msgRecvInfoSize);

struct IBTMsgInfo {
	IBTMsgType				msgType;
	IBT_CHAR				msgName[MAX_MSG_NAME_SIZE];
	IBTMsgProcessFunc		msgProcess;
	IBTBufToMsgParamFunc	bufToMsgParam;
	IBT_PTR					msgProcessParam;
	IBT_UI					msgProcessParamSize;
	IBT_CHAR				priv[0];
};

typedef struct {
	pthread_mutex_t		msgMutex;
	pthread_cond_t		msgCond;
	IBTList				msgRegList;
	IBTMsgRegStatus		msgRegStatus;
	IBTList				msgProcList;
	IBTMsgProcStatus	msgProcStatus;
	IBTMsgInfo			msgInfo;
} IBTMsg;

typedef struct {
	IBTMsg				*msg;
	IBT_UI				msgCnt;
} IBTMsgPool;

typedef struct {
	pthread_mutex_t		msgRegMutex;
	IBTListQueue		msgRegQueue;
} IBTMsgReg;

typedef struct {
	IBTListQueue		msgProcDoneQueue;
	sem_t				msgProcDoneSem;
	pthread_t			msgListenTid;
	pthread_t			msgProcessTid;
} IBTMsgProc;

struct IBTMsgMgrHandler {
	IBTHandler			*ibtHandler;
	IBTEventCallback event;
	IBTMsgRecv			msgRecv;
	IBTMsgPool			msgPool;
	IBTMsgReg			msgReg;
	IBTMsgProc			msgProc;
};

IBTMsgMgrHandler *IBT_MsgMgr_Init(IBTHandler *ibtHandler);

IBT_VOID IBT_MsgMgr_DeInit(IBTMsgMgrHandler *h);

IBT_INT IBT_MsgMgr_RegisterMsgs(IBTMsgMgrHandler *h, IBTMsgInfo *msgInfo);

IBT_INT IBT_MsgMgr_UnRegisterMsgs(IBTMsgMgrHandler *h, IBTMsgType msgType, IBT_BOOL isFreeParam);

IBT_INT IBT_MsgMgr_SendMsgs(IBTMsgMgrHandler *h, IBTMsgRecvInfoCtx *msgCodeCtx, IBT_INT timeOutMs);

IBT_INT IBT_MsgMgr_ImmediateResponseMsgs(IBTMsgMgrHandler *h, IBTMsgRecvInfoCtx *msgCodeCtx);

#endif
